package com.newbee.activity.tab;

import java.util.List;

import org.jivesoftware.smack.XMPPException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.activity.AddFriendsActivity;
import com.newbee.activity.LoginActivity;
import com.newbee.adapter.FriendsListvewAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.app.SpeechApp;
import com.newbee.listbean.FriendsInfoBean;
import com.newbee.listview.FriendsListview;
import com.newbee.listview.onListViewRefreshListener;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;

public class NewFriendsActivity extends Activity implements OnRefrushActivity,
		onListViewRefreshListener, OnClickListener
{

	private FriendsListvewAdapter fla;
	private FriendsListview mListView;
	private TextView emptyTextView;
	private final int REQUEST_CODE = 1;
	private long mExitTime;

	private Builder builder;

	private ImageButton addFrined;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.acitivity_newfriends);
		SpeechApp.add(this);
		emptyTextView = (TextView) this.findViewById(R.id.friendlist_empty);
		mListView = (FriendsListview) this.findViewById(R.id.friend_listview);
		mListView.setOnRefreshListener(this);
		addFrined = (ImageButton) this.findViewById(R.id.btn_addfriend);
		addFrined.setOnClickListener(this);
		emptyTextView.setOnClickListener(this);
		// MainServiceTask mst = new MainServiceTask(TaskType.GET_FRIEND, null,
		// this.getClass().getName());
		// MainService.addActivityMap(this.getClass().getName(), this);
		// MainService.addTaskQueue(mst);
		builder = new AlertDialog.Builder(this);
		builder.setMessage("亲，请先登录或注册！");
		builder.setTitle("提醒");
		builder.setPositiveButton("登录", new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick(DialogInterface arg0, int arg1)
			{

			}
		});

	}

	@Override
	protected void onStart()
	{

		super.onStart();
		// Toast.makeText(this, "onstart^11111111111111111111",
		// Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onResume()
	{
		super.onResume();

		SharedPreferences sp = this.getSharedPreferences(
				MainActivity.SP_USR_LOGIN_STATUS, Activity.MODE_MULTI_PROCESS);

		boolean isUserLog = sp.getBoolean(MainActivity.LOGIN_STATUE, false);

		if (isUserLog)
		{

			addFrined.setVisibility(View.VISIBLE);
			MainServiceTask mst = new MainServiceTask(TaskType.GET_FRIEND,
					null, this.getClass().getName());
			MainService.addActivityMap(this.getClass().getName(), this);
			MainService.addTaskQueue(mst);
		}
		else
		{

			// this.builder.create().show();

			userNoLogSet();
		}
	}

	private void userNoLogSet()
	{
		emptyTextView.setText("您未登录，请点击登录！");
		mListView.setEmptyView(emptyTextView);
		addFrined.setVisibility(View.GONE);
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.GET_FRIEND:
		{
			List<FriendsInfoBean> mlist = (List<FriendsInfoBean>) paras[0];
			if (mlist.size() == 0)
			{
				Log.d("wch", "-------------------->");

				emptyTextView.setText("您的好友列表为空");
				mListView.setEmptyView(emptyTextView);

			}
			else if (this.fla == null)
			{
				fla = new FriendsListvewAdapter(
						(List<FriendsInfoBean>) paras[0], this);
				mListView.setAdapter(fla);
			}
			else
			{
				fla.setList((List<FriendsInfoBean>) paras[0]);
				fla.notifyDataSetChanged();
			}

			break;
		}
		case TaskType.REFRUSH_FRIEND:
		{
			List<FriendsInfoBean> mlist = (List<FriendsInfoBean>) paras[0];
			if (mlist.size() == 0)
			{
				Log.d("wch", "-------------------->");

				mListView.setEmptyView(emptyTextView);

			}
			else if (this.fla == null)
			{
				fla = new FriendsListvewAdapter(
						(List<FriendsInfoBean>) paras[0], this);
				mListView.setAdapter(fla);
			}
			else
			{
				fla.setList((List<FriendsInfoBean>) paras[0]);
				fla.notifyDataSetChanged();
			}
			mListView.onRefreshComplete();

			break;
		}
		default:
			break;
		}
		return null;

	}

	@Override
	public void onRefresh()
	{
		MainServiceTask mst = new MainServiceTask(TaskType.REFRUSH_FRIEND,
				null, this.getClass().getName());
		MainService.addActivityMap(this.getClass().getName(), this);
		MainService.addTaskQueue(mst);
	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(this.getClass().getName());

		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_addfriend:
			Intent intent = new Intent(this, AddFriendsActivity.class);
			this.startActivityForResult(intent, REQUEST_CODE);
			break;
		case R.id.friendlist_empty:
		{
			SharedPreferences sp = this.getSharedPreferences(
					MainActivity.SP_USR_LOGIN_STATUS,
					Activity.MODE_MULTI_PROCESS);

			boolean isUserLog = sp.getBoolean(MainActivity.LOGIN_STATUE, false);
			if (isUserLog)
			{
				// do nothing
			}
			else
			{
				intent = new Intent(this, LoginActivity.class);
				startActivity(intent);

			}
			break;
		}

		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		if (requestCode == REQUEST_CODE
				&& resultCode == AddFriendsActivity.RESPONSE_CODE)
		{

			Bundle bundle = data.getExtras();
			String account = bundle.getString(AddFriendsActivity.ACCOUNT);
			if (SmackClient.connection.getUser().startsWith(account))
			{
				Toast.makeText(this, "您不能添加自己为好友！", Toast.LENGTH_LONG).show();
				return;
			}
			SmackClient sc = SmackClient.getInstance();
			try
			{
				sc.addFriend(account);
				Toast.makeText(this, "添加好友请求已发送……", Toast.LENGTH_LONG).show();
			}
			catch (XMPPException e)
			{
				Toast.makeText(this, "添加好友请求异常!", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if ((System.currentTimeMillis() - mExitTime) > 2000)
			{
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();

				return true;
			}
			else
			{
				SpeechApp.exit();
				return true;
			}

		}
		return super.onKeyDown(keyCode, event);
	}
}
